package com.ruoyi.web.controller.pay.ma;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.easy.query.api.proxy.client.EasyEntityQuery;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.order.OrderNoGenerator;
import com.ruoyi.web.controller.pay.ma.vo.PayCallbackReqVO;
import com.ruoyi.web.controller.pay.ma.vo.PayCreateReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 码支付
 * https://pay.payma.cn/
 */
@Api(value = "用户和套餐关联管理", tags = "用户和套餐关联管理")
@RestController
@Slf4j
@RequestMapping("/ma-pay")
public class PayMaController extends BaseController {

    @Autowired
    private EasyEntityQuery easyEntityQuery;
    @Autowired
    private RuoYiConfig ruoYiConfig;

    @ApiOperation("新增套餐信息")
    @Log(title = "套餐信息", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult add(@RequestBody PayCreateReqVO req) {


        String outNo = OrderNoGenerator.generateOrderNo1();
        String pay = MaPayUtil.pay(outNo, "测试", ".0.1"
                , req.getPayType(), "https://61826cc7.r40.cpolar.top/pay/callback");

        JSONObject jsonObject = JSONObject.parseObject(pay);
        String code = jsonObject.getString("code");
        if ("1".equals(code)) {
            Map<String, Object> map = new HashMap<>();
            map.put("out_trade_no", outNo);
            map.put("img", jsonObject.getString("img"));
            return AjaxResult.success(map);
        } else {
            return AjaxResult.error(jsonObject.getString("msg"));
        }


    }

    @ApiOperation("支付回调")
    @Log(title = "套餐信息", businessType = BusinessType.INSERT)
    @GetMapping("/callback")
    @Anonymous
    public String callback(PayCallbackReqVO req) {
        //成功
        log.info("支付回调进来了:{}", JSONObject.toJSONString(req));

        if (req.getTrade_status().equals("TRADE_SUCCESS")) {
            Map<String, Object> sign = new HashMap<>();
            sign.put("pid", req.getPid());
            sign.put("name", req.getName());
            sign.put("money", req.getMoney());
            sign.put("out_trade_no", req.getOut_trade_no());
            sign.put("trade_no", req.getTrade_no());
            sign.put("type", req.getType());
            sign.put("trade_status", req.getTrade_status());

            String signStr =MaPayUtil.getSign(sign);

            if (signStr.equals(req.getSign())) {
                //签名正确
                log.info("支付回调:{}:签名验证成功", req.getOut_trade_no());
                //处理自己端业务

            } else {
                log.info("支付回调:{}:签名验证错误{}", req.getOut_trade_no(), JSONObject.toJSONString(sign));
            }
        }
        return "success";
    }



}
